# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Brian Plattenburg <alphastryk@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import unittest
import sys
import yaml

sys.path.append('../')

from compound import Compound
from testCommon import *
import compoundData

class CompoundTest(unittest.TestCase):

	def setUp(self):
		setupDB()
		return

	def tearDown(self):
		# Not Yet Implemented
		return

	def testInsert(self):
		comp = Compound(compound1)
		comp.insert()
		return

	def testUpdate(self):
		comp = Compound(compound1)
		comp.insert()
		comp.forceUpdate()
		return

	def testLoadFunction(self):
		comp = Compound(compound1)
		comp.insert()
		comp.load()
		return

	def testLoadValidity(self):
		comp = Compound(compound1)
		comp.insert()
		comp2 = Compound.load(comp)
		self.AssertEqual(comp, comp2)
		return
		
class CompoundYamlTest(unittest.TestCase):
	
	def testLoadStringYaml(self):
		for i in range(len(compoundData.compoundYamls)):
			yml = compoundData.compoundYamls[i]
			comp = Compound()
			comp._loadYAML(yml)
			goodYaml = yaml.load(yml)
			self.assertEqual(comp.name, goodYaml['name'])
			self.assertEqual(comp.inchi, goodYaml['inchi'])
			self.assertEqual(comp.molfile, goodYaml['molfile'])
			#self.assertEqual(comp.fingerprint, goodYaml['fingerprint'])
		return
		
	def testDumpYaml(self):
		for i in range(len(compoundData.compoundYamls)):
			yml = compoundData.compoundYamls[i]
			comp = Compound()
			comp._loadYAML(yml)
			compDump = comp._dumpYAML()
			goodDump = yaml.dump(comp._things)
			self.assertEqual(compDump, goodDump)
		return
		
		
class CompoundDBTest(unittest.TestCase):

	def setUp(self):
		setupDB()

	def testInsertGet(self):
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			d = yaml.load(resp)
			self.assert_("key" in d.keys())
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':d['key']})
			dataGot = yaml.load(resp)
			data = yaml.load(i)
			for key in data:
				self.assertEqual(data[key], dataGot[key], "for %s had %s got %s" % (key, data[key], dataGot[key]))

	def testUpdateName(self):
		keys = []
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"name":"compoundname"+i})
			dbHandle.postAction("update", body, {'type':'Compound', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['name'], "compoundname"+i)

	def testUpdateFormula(self):
		keys = []
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"formula":"C6H5O6"})
			dbHandle.postAction("update", body, {'type':'Compound', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['formula'], "C6H5O6")

	def testUpdateInchi(self):
		keys = []
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"inchi":"blahblahblahblah"})
			dbHandle.postAction("update", body, {'type':'Compound', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['inchi'], "blahblahblahblah")

	def testUpdateIUPACName(self):
		keys = []
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"IUPACname":"2-methly-stupid"+i})
			dbHandle.postAction("update", body, {'type':'Compound', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['IUPACname'], "2-methly-stupid"+i)

	def testUpdateFingerprint(self):
		keys = []
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"fingerprint":[1,2,3,4,5,6]})
			dbHandle.postAction("update", body, {'type':'Compound', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['fingerprint'], [1,2,3,4,5,6])

	def testUpdateMolfile(self):
		keys = []
		for i in compoundData.compoundYamls:
			resp = dbHandle.postAction("insert", i, {'type':"Compound"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"molfile":";lkasdf;lj\n\nlakdl;adf;lajf\n\n;laksdf;lasf\nlkasd;lakjsdf"})
			dbHandle.postAction("update", body, {'type':'Compound', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Compound", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['molfile'], ";lkasdf;lj\n\nlakdl;adf;lajf\n\n;laksdf;lasf\nlkasd;lakjsdf")

	def testSearchName(self):
		print "Not Implemented"
		pass

	def testSearchInchi(self):
		print "Not Implemented"
		pass

	def testSearchIUPACname(self):
		print "Not Implemented"
		pass

	def testSearchFormula(self):
		print "Not Implemented"
		pass

	def testSubStructSearch(self):
		print "Not Implemented"
		pass


	def tearDown(self):
		clearDB()
		return
	
	
if __name__ == "__main__":
	unittest.main()   
